package com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.entity.EntrustedAgreement;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lianhong
 * @since 2022/6/15
 */
@Repository
public class EntrustedAgreementDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String deleteEntrustedAgreementSql = "DELETE FROM sw_entrusted_agreement WHERE seqNo IN (:seqNos)";

    public void deleteBy(List<String> seqNos) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("seqNos", seqNos);
        namedParameterJdbcTemplate.update(deleteEntrustedAgreementSql, params);
    }

    private String insertEntrustedAgreementSql = "INSERT INTO sw_entrusted_agreement (seqNo, consignNo, agentCode, declDate, consignNoL, entryId, entrustedAgreement, storageTime, socialCreditCode)" +
            " VALUES (:seqNo, :consignNo, :agentCode, :declDate, :consignNoL, :entryId, :entrustedAgreement, :storageTime, :socialCreditCode)";

    public void save(List<EntrustedAgreement> entrustedAgreementList) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(entrustedAgreementList);
        namedParameterJdbcTemplate.batchUpdate(insertEntrustedAgreementSql, batch);
    }
}
